package com.colabo.j2ee.web.core.dictionary;

import java.util.ArrayList;
import java.util.List;

import com.colabo.j2ee.web.core.context.ContextUtil;
import com.colabo.j2ee.web.core.context.CoreInitInfo;
import com.colabo.j2ee.web.core.exception.ServiceException;
import com.colabo.j2ee.web.core.oldCache.CacheManager;


public class CodeInfoCache {
	private static CodeInfoCache cic = null;

	private static CacheManager cache = new CacheManager();

	private final static String GROUP = "codeinfo";

	private CodeInfoCache() {
	}

	public synchronized static CodeInfoCache newInstance() {
		if (cic == null) {
			cic = new CodeInfoCache();
			// 加载数据
			CoreInitInfo app = CoreInitInfo.getInstance();
			String isLazy = app.getSystemConfig("codeinfo.lazyloading");
			if (isLazy != null && isLazy.equals("0")) {
				initCodeInfo();
			}
		}

		return cic;
	}

	public List<?> getCodeInfoListByTypeCode(String codeType) {
		lazyInitCodeInfo(codeType);

		return (List<?>) cache.getObj("codeinfotype" + codeType);
	}

	public IPCodeInfoVO getCodeInfoVO(String codeType, String codeInfoCode) {
		lazyInitCodeInfo(codeType);
		return (IPCodeInfoVO) cache.getObj("codeinfocode" + codeType + "_" + codeInfoCode);
	}

	public synchronized void reloadCodeInfo() {
		initCodeInfo();
	}

	private void lazyInitCodeInfo(String codeType) {

		List<?> list = (List<?>) cache.getObj("codeinfotype" + codeType);
		if (list == null) {
			ICodeInfoService codeService = (ICodeInfoService) ContextUtil.getBean(ICodeInfoService.BEAN_ID);
			if (codeService != null) {

				try {
					list = codeService.getCodeInfoByCodeType(codeType);
					cache.putGroup("codeinfotype" + codeType, list, GROUP);

					if (list != null) {
						for (int k = 0; k < list.size(); k++) {
							IPCodeInfoVO codeVO = (IPCodeInfoVO) list.get(k);
							if (codeVO != null) {
								cache.putGroup("codeinfocode" + codeType + "_" + codeVO.getCodeinfoCode(), codeVO,
										GROUP);
							}
						}
					}
				} catch (ServiceException e) {
					e.printStackTrace();
				}
			}
		}
	}

	private static void initCodeInfo() {
		ICodeInfoService codeService = (ICodeInfoService) ContextUtil.getBean(ICodeInfoService.BEAN_ID);
		if (codeService != null) {
			// hm = new HashMap();
			// codeHm = new HashMap();
			cache.flushGroup(GROUP);
			// cache.flushAll();
			try {
				/*
				 * List codeTypeList = codeService.getAllCodeTypes(); if
				 * (codeTypeList != null) { for (int i = 0; i <
				 * codeTypeList.size(); i++) { IPCodeTypeVO typeVO =
				 * (IPCodeTypeVO) codeTypeList.get(i); if (typeVO != null &&
				 * typeVO.getCodetypeCode() != null) { List codeInfoList =
				 * codeService.getCodeInfoByCodeType(typeVO.getCodetypeCode());
				 * 
				 * hm.put(typeVO.getCodetypeCode(), codeInfoList);
				 * 
				 * if(codeInfoList != null) { for(int k=0; k <
				 * codeInfoList.size(); k++) { IPCodeInfoVO codeVO =
				 * (IPCodeInfoVO)codeInfoList.get(k); if(codeVO != null) {
				 * codeHm.put(typeVO.getCodetypeCode()+"_"+codeVO.getCodeinfoCode(),
				 * codeVO); } } } } } }
				 */

				List<?> codeInfoList = codeService.getAllCodeInfo();
				if (codeInfoList != null) {
					for (int i = 0; i < codeInfoList.size(); i++) {
						IPCodeInfoVO codeVO = (IPCodeInfoVO) codeInfoList.get(i);

						if (codeVO != null) {
							IPCodeTypeVO typeVO = codeVO.getCodeTypeVO();
							if (typeVO != null && typeVO.getCodetypeCode() != null) {

								@SuppressWarnings("unchecked")
								List<IPCodeInfoVO> list = (List<IPCodeInfoVO>) cache.getObj("codeinfotype" + typeVO.getCodetypeCode());

								if (list == null) {
									list = new ArrayList<IPCodeInfoVO>();
									list.add(codeVO);
								} else {
									list.add(codeVO);
								}
								// hm.put(typeVO.getCodetypeCode(), list);
								cache.putGroup("codeinfotype" + typeVO.getCodetypeCode(), list, GROUP);
								cache.putGroup("codeinfocode" + typeVO.getCodetypeCode() + "_"
										+ codeVO.getCodeinfoCode(), codeVO, GROUP);
								// codeHm.put(typeVO.getCodetypeCode() + "_" +
								// codeVO.getCodeinfoCode(), codeVO);
							}
						}
					}
				}

			} catch (ServiceException e) {
				e.printStackTrace();
			}
		}
	}
}
